using System;
using System.Collections.Generic;
using System.IO;
using ZoneFiveSoftware.Common.Data;
using ZoneFiveSoftware.Common.Data.Fitness;
using ZoneFiveSoftware.Common.Data.Measurement;
using FilteredStatistics.Common.Controller;
using FilteredStatistics.Controller;
using FilteredStatistics.View;

namespace FilteredStatistics.Data
{
    class SeparateOnSplitBoundariesPostProcessFilter : IPostProcessFilter
    {
        public SeparateOnSplitBoundariesPostProcessFilter(Stream stream, DataVersion version)
        {
            Deserialize(stream, version);
        }

        public SeparateOnSplitBoundariesPostProcessFilter()
        {
        }

#region IPostProcessFilter Members

        public override PostProcessType Type
        {
            get { return PostProcessType.SeperateOnSplitBorder; }
        }

        public override String DisplayString
        {
            get
            {
                return PluginView.GetLocalizedString("SeperateOnSplitBoundariesPPText");
            }
        }

        public override List<ActivityRangeInfoCache> FilterResults(IList<ActivityRangeInfoCache> rangesToFilter, IActivity activity)
        {
            List<ActivityRangeInfoCache> result = new List<ActivityRangeInfoCache>();

            if (activity != null)
            {
                ActivityInfo infoCache = ActivityInfoCache.Instance.GetInfo(activity);
                IEnumerator<LapDetailInfo> lapsEnumerator = infoCache.RecordedLapDetailInfo.GetEnumerator();
                IEnumerator<ActivityRangeInfoCache> resultsEnumerator = rangesToFilter.GetEnumerator();
                DateTime lastSplitBorder = DateTime.MinValue;
                bool lapsEnumeratorIsValid = false;
                bool resultsEnumeratorIsValid = false;

                lapsEnumeratorIsValid = lapsEnumerator.MoveNext();
                resultsEnumeratorIsValid = resultsEnumerator.MoveNext();

                while (resultsEnumeratorIsValid)
                {
                    if (lapsEnumeratorIsValid)
                    {
                        IValueRange<DateTime> lapRange = new ValueRange<DateTime>(lapsEnumerator.Current.StartTime, lapsEnumerator.Current.EndTime);

                        if (lapRange.Lower <= resultsEnumerator.Current.RangeWithPauses.Lower)
                        {
                            lapsEnumeratorIsValid = lapsEnumerator.MoveNext();
                            lastSplitBorder = DateTime.MinValue;
                        }
                        else if (lapRange.Lower < resultsEnumerator.Current.RangeWithPauses.Upper)
                        {
                            DateTime segmentEnd;

                            if (lastSplitBorder == DateTime.MinValue)
                            {
                                lastSplitBorder = resultsEnumerator.Current.RangeWithPauses.Lower;
                                result.Add(new ActivityRangeInfoCache(new ValueRange<DateTime>(lastSplitBorder, lapRange.Lower),
                                                                      activity));
                            }

                            if (resultsEnumerator.Current.RangeWithPauses.Upper < lapRange.Upper)
                            {
                                segmentEnd = resultsEnumerator.Current.RangeWithPauses.Upper;
                                resultsEnumeratorIsValid = resultsEnumerator.MoveNext();
                            }
                            else
                            {
                                segmentEnd = lapRange.Upper;
                            }

                            result.Add(new ActivityRangeInfoCache(new ValueRange<DateTime>(lapRange.Lower, segmentEnd),
                                                                  activity));

                            lastSplitBorder = segmentEnd;
                            lapsEnumeratorIsValid = lapsEnumerator.MoveNext();
                        }
                        else
                        {
                            if (lastSplitBorder < resultsEnumerator.Current.RangeWithPauses.Upper)
                            {
                                result.Add(new ActivityRangeInfoCache(new ValueRange<DateTime>(lastSplitBorder, resultsEnumerator.Current.RangeWithPauses.Upper),
                                                                      activity));
                            }

                            resultsEnumeratorIsValid = resultsEnumerator.MoveNext();
                            lastSplitBorder = DateTime.MinValue;
                        }
                    }
                    else
                    {
                        if (!lastSplitBorder.Equals(DateTime.MinValue))
                        {
                            if (!lastSplitBorder.Equals(resultsEnumerator.Current.RangeWithPauses.Upper))
                            {
                                result.Add(new ActivityRangeInfoCache(new ValueRange<DateTime>(lastSplitBorder, resultsEnumerator.Current.RangeWithPauses.Upper),
                                                                      activity));
                            }
                        }
                        else
                        {
                            result.Add(resultsEnumerator.Current);
                        }

                        resultsEnumeratorIsValid = resultsEnumerator.MoveNext();
                    }
                }
            }
            else
            {
                result.AddRange(rangesToFilter);
            }

            return result;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            SeparateOnSplitBoundariesPostProcessFilter filter = obj as SeparateOnSplitBoundariesPostProcessFilter;

            if (filter != null)
            {
                return true;
            }

            return base.Equals(obj);
        }

        public override int CompareTo(IPostProcessFilter filter)
        {
            if (filter is RemoveSmallDistancesPostProcessFilter ||
                filter is RemoveSmallTimesPostProcessFilter ||
                filter is RemoveAscentRangeSectionsPostProcessFilter ||
                filter is RemoveDescentRangeSectionsPostProcessFilter)
            {
                return 1;
            }
            else if (filter is SeparateOnSplitBoundariesPostProcessFilter)
            {
                return 0;
            }

            return -1;
        }

#endregion

#region IPluginSerializable members

        public override void Serialize(Stream stream)
        {
            base.Serialize(stream);
        }

#endregion

#region Deserialization methods

        public override void Deserialize_V2(Stream stream, DataVersion version)
        {
        }

#endregion

    }
}
